<html><head><title>Sensor Management</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type = 'text/javascript'>
function loadXMLDoc(str)
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			xmldoc = xmlhttp.responseXML;
			xmlval = xmldoc.getElementsByTagName("element");
			document.getElementById("sensor_name").innerHTML = xmlval[0].childNodes[0].nodeValue;
			document.getElementById("region").innerHTML = xmlval[1].childNodes[0].nodeValue;
			document.getElementById("address").innerHTML = xmlval[2].childNodes[0].nodeValue;
			document.getElementById("comments").innerHTML = xmlval[3].childNodes[0].nodeValue;
			var inc = xmlval[4].childNodes[0].nodeValue;
			var rtnVal = "";
			if (inc != '0')
				rtnVal = "<select name = 'record' size = '6' onChange = 'loadState(this.value)'>";
			for (i=0; i<inc; i++)
			{
				rtnVal = rtnVal + "<option value = '" + i.toString(10) + "'>";
				rtnVal = rtnVal + xmlval[5+i].childNodes[0].nodeValue + "</option>";
			}
			if (inc != 0)
				rtnVal = rtnVal + "</select>";
			document.getElementById("disable_one").innerHTML = "";
			document.getElementById("imports").innerHTML = rtnVal;
			var array = Array();
			for (inc=5+i; inc<xmlval.length; inc++)
			{
				array[inc - (5 + i)] = xmlval[inc].childNodes[0].nodeValue;
			}
			setState(array);
		}
	}
	xmlhttp.open("GET", "sensor_info.php?sensor_name="+str, true);
	xmlhttp.send();
}
function setState(array)
{
	ifEnabled = array;
}
function loadState(str)
{
	if (ifEnabled[str] == 0)
	{
		document.getElementById("disable_one").innerHTML = "<td>Disable Selected</td><td><input type = 'checkbox' name = 'disable' checked = 'yes'/></td>"
	}
	else
	{
		document.getElementById("disable_one").innerHTML = "<td>Disable Selected</td><td><input type = 'checkbox' name = 'disable'/></td>";
	}
}
</script></head><body>
<?php
include "validate.php";
include "header.php";

if (isset($_SESSION["message"]))
{
	echo "<h4><font color = 'red'>" . $_SESSION["message"] . "</font></h4>";
	$_SESSION['message'] = NULL;
	unset($_SESSION['message']);
}

$query = "	SELECT name  
		FROM sensor;";
			
$result = mysql_query($query);

echo "<h2>Sensor Management</h2><form name = 'sensor_form' action = 'sensor_update.php' method = 'post'><table><tr><td><select name = 'sensor' size = '6' onChange = 'loadXMLDoc(this.value)'>";

while ($row = mysql_fetch_array($result))
{
	echo "<option value = '" . htmlspecialchars($row['name']) . "'>" . 
		htmlspecialchars($row['name']) . "</option>";
}

?>

</select></td><td><table><tr><td>
Sensor Name</td><td><div id = "sensor_name"></div>
</td></tr><tr><td>
Region</td><td><div id = "region"></div>
</td></tr><tr><td>
Address</td><td><div id = "address"></div>
</td></tr><tr><td>
Comments</td><td><div id = "comments"></div>
</td></tr></table></td></tr></table>
<table><tr><td><div id = "imports"></div></td><td><table><tr><td>
Disable All</td><td>
<input type = "checkbox" name = "disable_all"/></td></tr>
<tr><div id = "disable_one"></div></tr></table></td></tr></table>
<input type = "submit" value = "Update"/></form>
<form action = "update_database.php" method = "post">
<h4><font color = 'red'>Warning, this can take a long time! It is suggested that the public portal be taken down first.</font></h4>
<input type = "submit" value = "Update Database"/></form>
</body></html>
